package com.bolang.onlycode.demo.MyThread;

import java.util.List;

public class Producer implements Runnable{
    private List<Water>  waterList=null;
    public  Producer(List<Water> waterList){
        this.waterList=waterList;
    }
    @Override
    public void run() {
        while (true){
            synchronized (waterList){
                while (waterList.size()==2){
                    try {
                        System.out.println("开始wait");
                        waterList.wait();
                        System.out.println("wait方法后执行了");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                waterList.add(new Water());
                System.err.println(Thread.currentThread().getName()+"生产了1L水，现在水桶有：" + waterList.size() + "L水");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                /**
                 * 通知所有的消费者来消费
                 */
                waterList.notifyAll();
            }
        }

    }
}
